<template>
  <div class="userlist">
    <el-card class="box-card">
      <!-- 搜索添加栏 -->
      <div style="display:flex" slot="header" class="clearfix">
        <el-input
          style="width:300px"
          placeholder="请输入内容"
          v-model="search"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="append(search)"
          ></el-button>
        </el-input>
        <el-button style="margin-left:20px" type="primary" @click="add"
          >添加用户</el-button
        >
      </div>

      <!-- 用户列表信息 -->
      <el-table :data="list" border>
        <el-table-column prop="id" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="address" label="状态">
          
          <!-- 切换状态按钮 -->
          <el-switch
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="userStateChanged(scope.row)"
          >
          </el-switch
        ></el-table-column>
        <el-table-column prop="address" label="操作">
          <!-- 编辑按钮 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="edit(list.id)"
          ></el-button>

          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="del(id)"
          ></el-button>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加用户模态框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible1">
      <el-form :model="addform">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="addform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="addform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addform.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改用户模态框 -->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible2">
      <el-form :model="addform">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="addform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="addform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addform.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Userlist",
  data() {
    return {
      search: "",

      // 用户列表
      list: [
        {
          id: "1",
          username: "",
          email: "",
          mobile: "",
          role_name: "",
          addres: "",
          status: false
        }
      ],

      // 分页
      query: "",
      pagenum: 1,
      pagesize: 2,
      total: 0,
      value: true,

      // 添加用户
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      addform: {
        username: "",
        password: "",
        email: ""
      },
      formLabelWidth: "120px"
    };
  },
  mounted() {
    // 显示用户列表
    this.getuserlist();
  },
  methods: {
    // 获取列表数据
    getuserlist() {
      var url = this.$store.state.baseUrl;
      this.$axios
        .get(
          `${url}users?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
        )
        .then(res => {
          console.log(res);
          if (res.data.meta.status == 200) {
            this.list = res.data.data.users;
            this.total = res.data.data.total;
            // console.log(this.list);
          }
        });
    },

    //搜索
    append(search) {
      console.log(search);
      // 显示第一页
      this.pagenum = 1;
      this.getuserlist();
    },

    //添加模态框显示
    add() {
      this.dialogFormVisible1 = true;
    },

    // 取消添加
    cancle() {
      this.dialogFormVisible1 = false;
      this.dialogFormVisible2 = false;
    },

    // 确定添加
    sure() {
      this.dialogFormVisible1 = false;
      this.$axios.post("/users", this.addform).then(res => {
        console.log(res);
        if (res.data.meta.status == 201) {
          // console.log('success');

          // 正常添加
          // this.getuserlist();

          // 循环添加
          for (var i = 0; i < 50; i++) {
            this.list.username = "0" + i;
            this.getuserlist();
          }
          this.$message({
            message: "添加成功",
            type: "success"
          });
          this.addform.username = "";
          this.addform.password = "";
          this.addform.email = "";
        }
      });
    },

    // 编辑
    edit(id) {
      console.log(id);
      this.dialogFormVisible2 = true;
    },

    // 删除
    del(id) {
      this.$confirm("确认删除吗？")
        .then(() => {
          var url = this.$store.state.baseUrl;
          this.$axios.delete(`${url}users/${id}`).then(res => {
            console.log(res);
            if (res.meta.status == 201) {
              this.$message.success("删除成功");
              this.getuserlist();
            } else {
              this.$message.error(res.meta.msg);
            }
          });
        })
        .catch(() => {});
    },

    //显示页数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.getuserlist();
    },

    //显示每页条数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.getuserlist();
    },

    //修改状态
    userStateChanged(row) {
      var { data: res } = this.$axios.put(
        `users/${row.id}/state/${row.mg_state}`
      );
      if (res.meta.status !== 200) {
        row.mg_state = !row.mg_state;
        return this.$message.error("修改状态失败");
      }
      this.$message.success("更新状态成功");
    }
  }
};
</script>

<style lang="scss">
.userlist {
  width: 100%;
  // height: 100%;
  display: flex;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
  // height: 800px;
}
.el-table__header-wrapper,
.el-table__body-wrapper,
.is-scrolling-left {
  width: 100%;
}
table {
  width: 100% !important;
}
.el-table__header {
  width: 100%;
  // height: 50px;
}
.el-pagination {
  margin-top: 15px;
  width: 100%;
  display: flex;
  // align-items: center;
}
</style>